from django.contrib import admin
from django.utils.html import format_html
from django.conf import settings
from .models import TextGenerationRecord, PromptTemplate


@admin.register(TextGenerationRecord)
class TextGenerationRecordAdmin(admin.ModelAdmin):
    """文本生成记录管理"""
    
    list_display = [
        'id',
        'module_display',
        'content_type_display', 
        'language_style_display',
        'prompt_preview',
        'user',
        'created_at',
        'is_favorite'
    ]
    
    list_filter = [
        'module',
        'content_type', 
        'language_style',
        'created_at',
        'is_favorite'
    ]
    
    search_fields = ['prompt', 'generated_content', 'user__username']
    
    readonly_fields = ['created_at', 'used_model']
    
    # 详情页分组显示
    fieldsets = (
        ('基础信息', {
            'fields': ('user', 'module', 'content_type', 'language_style', 'used_model')
        }),
        ('生成内容', {
            'fields': ('prompt', 'generated_content')
        }),
        ('生成参数', {
            'fields': ('generation_params', 'is_favorite')
        }),
        ('时间信息', {
            'fields': ('created_at',)
        }),
    )
    
    # 按创建时间倒序排列
    ordering = ['-created_at']
    
    def prompt_preview(self, obj):
        """提示词预览"""
        return obj.prompt_preview()
    prompt_preview.short_description = '提示词预览'
    
    def module_display(self, obj):
        """功能模块显示"""
        return obj.get_module_display()
    module_display.short_description = '功能模块'
    
    def content_type_display(self, obj):
        """内容类型显示"""
        return obj.get_content_type_display()
    content_type_display.short_description = '内容类型'
    
    def language_style_display(self, obj):
        """语言风格显示"""
        return obj.get_language_style_display()
    language_style_display.short_description = '语言风格'
    
    # 添加自定义操作
    actions = ['mark_as_favorite', 'unmark_as_favorite']
    
    def mark_as_favorite(self, request, queryset):
        """标记为收藏"""
        updated = queryset.update(is_favorite=True)
        self.message_user(request, f'成功标记 {updated} 条记录为收藏')
    mark_as_favorite.short_description = '标记为收藏'
    
    def unmark_as_favorite(self, request, queryset):
        """取消收藏"""
        updated = queryset.update(is_favorite=False)
        self.message_user(request, f'成功取消 {updated} 条记录的收藏')
    unmark_as_favorite.short_description = '取消收藏'


@admin.register(PromptTemplate)
class PromptTemplateAdmin(admin.ModelAdmin):
    """提示词模板管理"""
    
    list_display = [
        'content_type_display',
        'language_style_display',
        'template_preview',
        'is_active',
        'created_at',
        'updated_at'
    ]
    
    list_filter = [
        'content_type',
        'language_style',
        'is_active',
        'created_at'
    ]
    
    search_fields = ['template']
    
    readonly_fields = ['created_at', 'updated_at']
    
    fieldsets = (
        ('基础信息', {
            'fields': ('content_type', 'language_style', 'is_active')
        }),
        ('模板内容', {
            'fields': ('template',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at')
        }),
    )
    
    def content_type_display(self, obj):
        """内容类型显示"""
        return obj.get_content_type_display()
    content_type_display.short_description = '内容类型'
    
    def language_style_display(self, obj):
        """语言风格显示"""
        return obj.get_language_style_display()
    language_style_display.short_description = '语言风格'
    
    def template_preview(self, obj):
        """模板预览"""
        return obj.template[:50] + '...' if len(obj.template) > 50 else obj.template
    template_preview.short_description = '模板预览'